package io.renren.modules.erp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.erp.service.BoxInfoService;
import io.renren.modules.erp.service.BusinessBillInfoService;
import io.renren.modules.erp.utils.ExportExcelUtil;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-07-19 22:30:09
 */
@RestController
@RequestMapping("erp/export")
public class ExportExcelController {
    @Autowired
    private BoxInfoService boxInfoService;
    
    @Autowired
    private BusinessBillInfoService businessBillInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("erp:boxinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = boxInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping(value = "/exportBillInfoData", method = RequestMethod.GET)
	public void exportBillInfoData(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> params) {
		try {
			String startDate = (String) params.get("startDate");
			String endDate = (String) params.get("endDate");
			if (!StringUtils.isEmpty(startDate)) {
				String startString = String.format("%s 00:00:00", startDate);
				params.put("effectiveStart", startString);
			}
			if (!StringUtils.isEmpty(endDate)) {
				String endString = String.format("%s 23:59:59", endDate);
				params.put("effectiveEnd", endString);
			}

			List<Map<String, Object>> excelData = businessBillInfoService.queryBillInfoPage(params);

			List<List<String>> excelList = new ArrayList<List<String>>();
			for (Map<String, Object> map : excelData) {
				List<String> list = new ArrayList<String>();
				list.add(map.get("bill_code") + "");
				list.add(convertNullToEmpty(map.get("bill_date") + ""));
				list.add(map.get("customer_code") + "");
				list.add(map.get("delivery_address") + "");
				list.add(map.get("delivery_part") + "");
				list.add(map.get("customer_po") + "");
				list.add(map.get("payment_method") + "");
				list.add(map.get("currency_type") + "");
				list.add(map.get("customer_goods_code") + "");
				list.add(map.get("box_code") + "");
				list.add(map.get("pimo_code") + "");
				list.add(map.get("paper_code") + "");
				list.add(map.get("change_paper_code") + "");

				excelList.add(list);
			}

			ExportExcelUtil.outputExcel(request, response, excelList, "订单记录.xls", "trade_data.xls");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

    private String convertNullToEmpty(String str) {
		if (str == null || "null".equals(str)) {
			return "";
		} else {
			return str;
		}
	}

}
